#include "all.h"
#include "StdSqlite.h"
Sql* ser_db;
void InitUser(user_act* user)
{
     user = (user_act*)malloc(sizeof(user_act));
    if(user == NULL)
    {
        printf("user_act malloc error!\n");
        return  ;
    }
    memset(user->ID,0,20);
    memset(user->Name,0,20);
    memset(user->passwd,0,20);
    memset(user->Ccontent,0,20);
    memset(user->Scontent,0,20);

    user->status = 1 ;
    user->choice= -1 ;
    
    return ;
}

struct Client
{
    char name[20];
    int sock;
};
typedef struct Client ClientLList;
ClientLList *CreateCLList(const char *name, int sock)
{
    ClientLList* c = (ClientLList*)malloc(sizeof(ClientLList));
    if(c == NULL)
    {
        perror("CLL malloc :");
        return NULL;
    }
    strcpy(c->name,name);
    c->sock = sock;
    return c;
}
void FreeCLList(ClientLList* c)
{
    free(c);
    return ;
}

DLList ClientList;

struct message
{
    int flag;
    char fromName[20];
    char toName[20];
    char content[1024];
};
typedef struct message Msg;

void* thread_handler(void* arg)
{
    
    int sock = *((int *)arg);
    while(1)
    {
        user_act u  ;
        //InitUser(&u);
        
        Msg m ;
       
        TCPServerRecv(sock,&u,sizeof(user_act));  
        // if (sqlite3_open("test.db", &ser_db->db) != SQLITE_OK)
        // {
        //     printf("open database error : %s\n", sqlite3_errmsg(ser_db->db));
        // }
        switch(u.choice)
        {
            case 1://log
                if(ConfirmById(ser_db,"User",u.ID,u.Name,u.passwd) == true)
                {
                    printf("%s登录成功!\n",u.Name);
                    memset(u.Scontent,0,sizeof(u.Scontent));
                    sprintf(u.Scontent,"登录成功!\n");
                    InsertTail(&ClientList,CreateCLList(u.ID,sock));
                    TcpServerSend(sock,&u,sizeof(user_act));
                    break;
                }
                else
                {
                    u.choice = -1 ;
                    memset(u.Scontent,0,sizeof(u.Scontent));

                    printf("账户或密码错误，请重试\n");
                    sprintf(u.Scontent,"账户或密码错误，请重试\n");
                    TcpServerSend(sock,&u,sizeof(user_act));                  
                }
                break;         
            case 2://resgister
                char*  prolist[100]= {0} ;
                prolist[0] = "NULL";
                prolist[1] = malloc(20);
                prolist[2] = malloc(20);
                sprintf(prolist[1],"'%s'",u.Name);
                sprintf(prolist[2],"'%s'",u.passwd);
                InsertData(ser_db,"User",prolist,3);//sizeof(prolist)/sizeof(char*)
                char* id =  GetIDFromTableINRes(ser_db,"User",u.Name,u.passwd);
                memset(u.Scontent,0,sizeof(u.Scontent));
                sprintf(u.Scontent,"\n注册成功！\n请记好您的登录信息\n聊天号：%s\n账号名称：%s\n密码：%s\n",id,u.Name,u.passwd);
                printf("%s",u.Scontent);
                TcpServerSend(sock,&u,sizeof(user_act));
                break;
            
            case 7 :
                char sql[400] = {0} ;
                sprintf(sql,"select id,name from stu;");
                char** result;
                int row,column;
                if( sqlite3_get_table(ser_db,sql,&result,&row,&column,NULL) != SQLITE_OK)
                {
                    printf("get table error : %s\n",sqlite3_errmsg(ser_db));
                }
                u.adr = result ;
                TcpServerSend(sock,&u,sizeof(user_act));
                break;

            case 87:
                    struct Node* TravelPoint = ClientList.head;
                    
                    while(TravelPoint != NULL)
                    {
                        ClientLList* c = (ClientLList*)TravelPoint->data;
                        if(strcmp(c->name,m.toName) == 0)
                        {
                            TcpServerSend(c->sock,&m,sizeof(m));
                            break;
                        }
                        TravelPoint = TravelPoint->next;
                    }
                break;
            default:
                break;
        }
    }
 }

int main(int argc,char* argv[])
{
    TcpS* s;

    if(argc != 3)
    {
        printf("invaild numbers!\n");
        return -1;
    }
    ThreadP* p = InitThreadPool(100,10,50);
    if(p == NULL)
    {
        printf("ThreadPool is error!\n");
        return -1;
    }
    InitDLList(&ClientList);

    s = InitTcpServer( argv[1],atoi(argv[2]));
    ser_db = InitSqlite("server.db"); 
    char*  prolist[] = {"id","integer","name","text","passwd","text"};
    CreateTable(ser_db,"User",prolist,sizeof(prolist)/sizeof(char*)/2);

    int acceptSock ;

    while((acceptSock = TcpServerAccept(s)) > 0)
    {
        ThreadP_AddTask (p,thread_handler,&acceptSock);
    }
    //ClearTcpServer(s);
    
    
    return 0;
}